1968D - Permutation Game - CodeForces Solution


games math

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
  n, k , pb, ps = map(int, input().split())
  p = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  s_set = { ps }
  b_set = { pb }

  max_b = a[pb - 1] * k
  total_b = a[pb - 1]
  
  max_s = a[ps - 1] * k
  total_s = a[ps - 1]
  if k > 1:
    pb = p[pb - 1]
    kb = k - 1
    while pb not in b_set and kb > 0:
      b_set.add(pb)
      max_b = max(max_b, total_b + a[pb - 1] * kb)
      total_b += a[pb - 1]
      pb = p[pb - 1]
      kb -= 1

    ps = p[ps - 1]
    ks = k - 1
    while ps not in s_set and ks > 0:
      s_set.add(ps)
      max_s = max(max_s, total_s + a[ps - 1] * ks)
      total_s += a[ps - 1]
      ps = p[ps - 1]
      ks -= 1

  if max_s == max_b:
    print("Draw")
  elif max_b < max_s:
    print("Sasha")
  else:
    print("Bodya")


Comments

Submit
0 Comments
More Questions

1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh